package com.swu.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.swu.po.DiseaseDictionary;
import com.swu.mapper.DiseaseDictionaryMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class DiseaseExcelListener extends AnalysisEventListener<DiseaseExcel> {

    private final DiseaseDictionaryMapper diseaseDictionaryMapper;

    public DiseaseExcelListener(DiseaseDictionaryMapper diseaseDictionaryMapper) {
        this.diseaseDictionaryMapper = diseaseDictionaryMapper;
    }

    @Override
    public void invoke(DiseaseExcel data, AnalysisContext context) {
        log.info("解析到一条疾病数据：{}", data);
        
        // 转换为实体对象
        DiseaseDictionary disease = new DiseaseDictionary();
        disease.setDiseaseCode(data.getDiseaseCode());
        disease.setDiseaseName(data.getDiseaseName());
        disease.setPinyinCode(data.getPinyinCode());
        disease.setWubiCode(data.getWubiCode());
        disease.setLength(data.getDiseaseName().length());
        
        // 插入数据库
        diseaseDictionaryMapper.insert(disease);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("疾病数据解析完成！");
    }
} 